package com.xzxy.xmlg_backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xzxy.xmlg_backend.entity.OperationLog;
import com.xzxy.xmlg_backend.entity.Result;
import com.xzxy.xmlg_backend.entity.dto.OperationLogDto;
import com.xzxy.xmlg_backend.entity.vo.OperationLogVo;
import com.xzxy.xmlg_backend.service.IOperationLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 企业智能组织管理系统操作日志表 前端控制器
 * </p>
 *
 * @author 周毅盛
 * @since 2025-06-13
 */
@RestController
@RequestMapping("/operationLog")
@Tag(name="日志管理")
public class OperationLogController {

    @Autowired
    private IOperationLogService iOperationLogService;


    /**
     * 分页查询日志信息
     * @param operationLogDto
     * @return
     */
    @GetMapping("/page")
    @Operation(summary = "日志分页",description = "日志分页查询信息")
    public Result page(OperationLogDto operationLogDto){
        Page<OperationLog> page = new Page<>(operationLogDto.getPage(),operationLogDto.getPageSize());
        LambdaQueryWrapper<OperationLog> wrapper = new LambdaQueryWrapper<>();
        if (operationLogDto.getUserName() != null && operationLogDto.getUserName()!= "")
            wrapper.eq(OperationLog::getUserName,operationLogDto.getUserName());
        if (operationLogDto.getOperationType() != null && operationLogDto.getOperationType()!= "")
            wrapper.eq(OperationLog::getOperationType,operationLogDto.getOperationType());
        if (operationLogDto.getOperationIp() != null && operationLogDto.getOperationIp()!= "")
            wrapper.eq(OperationLog::getOperationIp,operationLogDto.getOperationIp());
        if (operationLogDto.getOperationMethod() != null && operationLogDto.getOperationMethod()!= "")
            wrapper.eq(OperationLog::getOperationMethod,operationLogDto.getOperationMethod());
        if (operationLogDto.getOperationStatus() != null && operationLogDto.getOperationStatus()!= "")
            wrapper.eq(OperationLog::getOperationStatus,operationLogDto.getOperationStatus());
        
        // 添加时间范围查询条件
        if (operationLogDto.getStartTime() != null && !operationLogDto.getStartTime().isEmpty()) {
            wrapper.ge(OperationLog::getLastOperationTime, operationLogDto.getStartTime());
        }
        if (operationLogDto.getEndTime() != null && !operationLogDto.getEndTime().isEmpty()) {
            wrapper.le(OperationLog::getLastOperationTime, operationLogDto.getEndTime());
        }
        
        List<OperationLog> list = iOperationLogService.list(page, wrapper);
        OperationLogVo operationLogVo = new OperationLogVo();
        operationLogVo.setTotal(page.getTotal());
        operationLogVo.setRows(list);
        return Result.success(operationLogVo);
    }
}
